package domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class DAOContact implements IDAOContact{
	
	@Override
	public boolean addContact(String firstname, String lastname, String email,
			String street, String city, String zip, String country,Set<PhoneNumber> phones) {

		Contact contact = new Contact();
		contact.setEmail(email);
		contact.setFirstName(firstname);
		contact.setLastName(lastname);	
		
		Address address = new Address();
		
		address.setCity(city);
		address.setCountry(country);
		address.setStreet(street);
		address.setZip(zip);
		
		contact.setAddress(address);		
		contact.setPhones(phones);		
		
		//AJOUT
		Iterator<PhoneNumber>it = phones.iterator();
		while(it.hasNext())
		{
			PhoneNumber p = it.next();
			p.setContact(contact);
		}	
		
		
		Set<ContactGroup>groups = new HashSet<ContactGroup>();
		
		ContactGroup g1 = new ContactGroup();
		g1.setGroupName("GEEK");
		g1.setContacts(null);
		
		groups.add(g1);
		
		contact.setBooks(groups);
		
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();		
			
			session.save(contact);	
		
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
			return false;
		}
		
		return true;
		
	}

	
	@Override
	public boolean deleteContact(Contact contact) {
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
						
			session.delete(contact);
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
			return false;
		}
		
		return true;
	}
	
	@Override
	public boolean deleteContact(long id) {
		
		Contact contact;
		
		contact = this.getContact(id);
		if ( contact == null )
			return false;
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
						
			session.delete(contact);
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
			return false;
		}
		
		return true;
	}

	@Override
	public Contact getContact(long id) {
		Contact cta;

		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
			
			cta = (Contact)session.get( Contact.class, id );
			
			tx.commit();
			
			session.close();			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
			cta = null;
		}
		
		return cta;
	}	
	
	
	@Override
	public boolean modifyContact(long id, String firstname, String lastname,String email, String street, String city, String zip, String country,Set<PhoneNumber> phones) {
		
		Contact c ;
		Address a ;
		
		c = this.getContact(id);
		a = c.getAddress();
		
		if ( c == null )
			return false;
		
		c.setFirstName(firstname);
		c.setLastName(lastname);
		c.setEmail(email);
		
		a.setCity(city);
		a.setCountry(country);
		a.setStreet(street);
		a.setZip(zip);
		
		c.setPhones(phones);
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
									
			session.update(c);	
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
			return false;
		}	
		
		return true;
	}
	

	@Override
	public boolean modifyContact(Contact contact, String firstname,
			String lastname, String email, String street, String city, String zip, String country,Set<PhoneNumber> phones) {
		
		Contact c = contact;
		Address a = c.getAddress();			
		
		if ( c == null )
			return false;
		
		c.setFirstName(firstname);
		c.setLastName(lastname);
		c.setEmail(email);
		
		a.setCity(city);
		a.setCountry(country);
		a.setStreet(street);
		a.setZip(zip);		
		
		c.setPhones(phones);
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
								
			session.update(c);	
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
			return false;
		}
		
		return true;	
	}
	

	@Override
	public ArrayList<Contact> getContactByFirstname(String firstname) {	
		
		ArrayList<Contact> listeContact = new ArrayList<Contact>();
		
		StringBuffer requeteS = new StringBuffer();
		requeteS.append("from Contact as c  left join fetch c.address address where c.firstName='"+firstname+"'");
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
				
		
			Query req = session.createQuery(requeteS.toString());
			List resultats = req.list();
			
			Iterator it = resultats.iterator();
			while(it.hasNext()){
				Object o = it.next();
				if ( o instanceof Contact){
					Contact c = (Contact) o;
					listeContact.add(c);
				}
			}
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
		}
		
		return listeContact;

	}

	@Override
	public ArrayList<Contact> getContactByLastname(String lastname) {
		
		ArrayList<Contact> listeContact = new ArrayList<Contact>();
		
		StringBuffer requeteS = new StringBuffer();
		requeteS.append("from Contact as c  left join fetch c.address address where c.lastName='"+lastname+"'");
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
				
		
			Query req = session.createQuery(requeteS.toString());
			List resultats = req.list();
			
			Iterator it = resultats.iterator();
			while(it.hasNext()){
				Object o = it.next();
				if ( o instanceof Contact){
					Contact c = (Contact) o;
					listeContact.add(c);
				}
			}
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
		}
		
		return listeContact;
	}

	@Override
	public ArrayList<Contact> getContactByEmail(String email) {

		ArrayList<Contact> listeContact = new ArrayList<Contact>();
		
		StringBuffer requeteS = new StringBuffer();
		requeteS.append("from Contact as c  left join fetch c.address address where c.email='"+email+"'");
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
				
		
			Query req = session.createQuery(requeteS.toString());
			List resultats = req.list();
			
			Iterator it = resultats.iterator();
			while(it.hasNext()){
				Object o = it.next();
				if ( o instanceof Contact){
					Contact c = (Contact) o;
					listeContact.add(c);
				}
			}
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
		}
		
		return listeContact;
	}


	@Override
	public ArrayList<Contact> getAllExistingContact() {
		
		ArrayList<Contact> listeContact = new ArrayList<Contact>();
		
		StringBuffer requeteS = new StringBuffer();
		
		requeteS.append("from Contact as c left join fetch c.address address");
		
		try{
			
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			//Ouverture de la transaction
			org.hibernate.Transaction tx = session.beginTransaction();
				
		
			Query req = session.createQuery(requeteS.toString());
			List resultats = req.list();
			
			Iterator it = resultats.iterator();
			while(it.hasNext()){
				Object o = it.next();
				if ( o instanceof Contact){
					Contact c = (Contact) o;
					listeContact.add(c);
				}
			}
			
			tx.commit();
		
			session.close();
			
		} catch ( Exception e ){
			System.out.println("Erreur : "+e.getMessage());
		}
		
		return listeContact;
	}
	

}
